package org.zfes.snowier.cloud.zbss.cms.content.service.impl;

import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.cms.content.service.ICmsContentChargeService;
import org.zfes.snowier.cloud.zbss.cms.content.dao.CmsContentChargeMapper;
import org.zfes.snowier.cloud.zbss.cms.content.model.CmsContentCharge;
import java.util.stream.Stream;
@Service
public class CmsContentChargeServiceImpl implements ICmsContentChargeService{
 	@Autowired
	private CmsContentChargeMapper cmsContentChargeMapper;
	
 	//========================增
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createCmsContentCharge(CmsContentCharge cmsContentCharge) {
		ZBeanUtil.validateBean(cmsContentCharge);
		cmsContentCharge.setStatus(Byte.valueOf("1"));//状态:_D#1 启用 0禁用 -1删除#
		cmsContentCharge.setCreateTime(ZDateUtil.newDate());
		cmsContentCharge.setUpdateTime(ZDateUtil.newDate());
		cmsContentChargeMapper.insertSelective(cmsContentCharge);
	}
    //=====================删
	//====================删
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteCmsContentCharge(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		cmsContentChargeMapper.deleteByPrimaryKey(id);
	}
	//=====================改
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateCmsContentCharge(CmsContentCharge cmsContentCharge) {
		ZBeanUtil.validateBean(cmsContentCharge);
		cmsContentCharge.setUpdateTime(ZDateUtil.newDate());
		cmsContentChargeMapper.updateByPrimaryKeySelective(cmsContentCharge);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsContentChargeStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsContentChargeMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsContentChargeStatusByContentId(Long contentId, Byte status) {
		ZAssert.notNull(contentId, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsContentChargeMapper.updateStatusByContentId(contentId, status);
	}
	//=====================查
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsContentCharge> loadCmsContentChargeById(Long id) {
		if(id!=null){
			return Optional.ofNullable(cmsContentChargeMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadCmsContentChargeDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(cmsContentChargeMapper.selectListCount(pm), cmsContentChargeMapper.selectMapListPage(pm));
		return ds;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsContentCharge> loadCmsContentChargeByContentId(Long contentId) {
		if(contentId!=null){
			return cmsContentChargeMapper.selectByContentId(contentId);
		}
		return Optional.empty();
	}

	
}